package cn.hnucm.searchesservice.controller;


import cn.hnucm.searchesservice.entity.TbLog;
import cn.hnucm.searchesservice.service.TbLogService;
import cn.hnucm.searchesservice.utils.MyLog;
import cn.hnucm.searchesservice.utils.Result;
import cn.hnucm.searchesservice.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author chenjintao
 * @since 2021-12-20
 */
@Api(tags = "系统日志")
@RestController
@CrossOrigin
@RequestMapping("/serchesservice/tbCapital")
public class TbLogController {

    @Autowired
    @Qualifier("tbLogServiceImpl")
    private TbLogService service;

    /**
     * 获取所有的日志
     * @return
     */
    @MyLog("获取所有的日志")
    @ApiOperation("获取所有的日志")
    @GetMapping("/getAllLog")
    @ResponseBody
    public Result getAllLog(){
        List<TbLog> logs = service.getAllLog();
        if (logs == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (logs.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("logs",logs);
    }


    /**
     * 增加一条日志
     * @param tbLog
     * @return
     */
    @MyLog("增加一条日志")
    @ApiOperation("增加一条日志")
    @PostMapping("/addLog")
    @ResponseBody
    public Result addLog(@RequestBody TbLog tbLog){
        if (tbLog == null) return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        if (!service.addLog(tbLog)) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }

    /**
     * 通过日志Id删除一条日志
     * @param logId
     * @return
     */
    @MyLog("通过日志Id删除一条日志")
    @ApiOperation("通过日志Id删除一条日志")
    @GetMapping("/deleteLogById/{logId}")
    @ResponseBody
    public Result deleteLogById(@PathVariable("logId") Integer logId){
        if (logId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        if (!service.deleteLogById(logId)) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }

    /**
     * 通过日志Id查询一条日志
     * @param logId
     * @return
     */
    @MyLog("通过日志Id查询一条日志")
    @ApiOperation("通过日志Id查询一条日志")
    @GetMapping("/selectLogById/{logId}")
    @ResponseBody
    public Result selectLogById(@PathVariable("logId") Integer logId){
        if (logId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        TbLog tbLog = service.selectLogById(logId);
        if (tbLog == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok("log",tbLog);
    }

    /**
     * 批量查询日志,根据多层因素
     * @param userId
     * @param operation
     * @param method
     * @return
     */
    @MyLog("批量查询日志,根据多层因素")
    @ApiOperation("批量查询日志,根据多层因素")
    @GetMapping("/selectLog/{userId}/{operation}/{method}/{pageFrom}/{pageSize}")
    @ResponseBody
    public Result selectLog(@PathVariable("userId") Integer userId,
                          @PathVariable("operation") String operation,
                          @PathVariable("method") String method,
                          @PathVariable("pageFrom") Integer pageFrom,
                          @PathVariable("pageSize") Integer pageSize){
        if (pageFrom == null) pageFrom = 0;
        if (pageSize == null) pageSize = 20;
        List<TbLog> logs = service.selectLog(userId, operation, method, pageFrom, pageSize);
        if (logs == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (logs.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("logs",logs);
    }

}

